const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const AuthorSchema = new Schema({
    first_name: { type: String, required: true, max: 100 },
    family_name: { type: String, required: true, max: 100 },
    date_of_birth: { type: Date },
    date_of_death: { type: Date }
});

// 虚拟属性：作者全名
AuthorSchema.virtual('name').get(function () {
    return this.first_name + ', ' + this.family_name;
});

// 虚拟属性：作者寿命
AuthorSchema.virtual('lifespan').get(function () {
    return (this.date_of_death.getYear() - this.date_of_birth.getYear()).toString();
});

// 虚拟属性：作者URL
AuthorSchema.virtual('url').get(function () {
    return '/catalog/author/' + this._id;
});

// 导出 Author 模型
module.exports = mongoose.model('Author', AuthorSchema);
